package com.xiaobias.itheima.serialize;

import java.io.*;
import java.util.Arrays;

public class JdkSerialize {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
         Book book = new Book("1", "三国", "历史大剧", "人物", "兄弟创业");

         // encode
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);

        oos.writeObject(book);
        oos.flush();

        byte[] bytes = bos.toByteArray();
        System.out.printf("序列化的结果：%s, 大小为： %d 字节\n" , Arrays.toString(bytes), bytes.length);

        // decode
        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes));
        Book b = (Book) ois.readObject();
        System.out.printf("方序列化得到的对象: %s \n", b);
    }
}
